﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Base.Extensions;
using Base.Data;
using System.IO;
using System.Text;
using UnityEngine.UI;

public class RenlationRightMenu : MonoBehaviour {
    public Object 关系预制件;
    public RelationData data;
    public int codeSum;
    public int pathSum;
    private RelationData _data;
    private List<string> id;
    private List<string> kgid;
    private string code;
    // Use this for initialization
    void Start() {
        codeSum = Dictionaries.relationpath.Count + 1;
    }

    // Update is called once per frame
    void Update() {

    }
    public void 新建关系()
    {

        Transform form = 关系预制件.InstanUI(data.content);
        form.Find("Title/Pipeline/Title").SetText("新建关系");
        form.Find("Title/Valve/Title").SetText("未指定开关");
        _data = form.gameObject.AddComponent<RelationData>();
        _data.code = data.code + "@r" + codeSum.ToString();
        Relationship ship = data.transform.GetComponent<Relationship>();
        ship.foldOnClick(data.transform.Find("Title/Fold/Text").GetComponent<Text>());
        //_data.id = ;
        codeSum++;
        print(_data.code);
    }
    public void 选择路径()
    {
        id = new List<string>();
        foreach (var item in FreeCamera.关系集合)
        {
            id.Add(item.go.name);
        }
    }
    public void 指定开关()
    {
        kgid = new List<string>();
        foreach (var item in FreeCamera.关系集合)
        {
            id.Add(item.go.name);
        }
    }
    public void 保存关系()
    {
        string[] strs = data.code.Split('@');
        int index = Dictionaries.relationpath.FindIndex(s => s.code == strs[strs.Length - 1]);
        RelationPath path;
        if (index == -1)
        {

            path = new RelationPath();
            path.objID = id;
            path.on_offID = kgid;
            path.pahtname = data.transform.Find("Title/Pipeline/Title").GetText();
            path.kgname = data.transform.Find("Title/Valve/Title").GetText();
            path.code = strs[strs.Length - 1];
            Dictionaries.relationpath.Add(path);
        }
        else
        {
            path = Dictionaries.relationpath[index];
            path.objID = id;
            path.on_offID = kgid;
            path.pahtname = data.transform.Find("Title/Pipeline/Title").GetText();
            path.kgname = data.transform.Find("Title/Valve/Title").GetText();
        }
        保存关系数据();
        int index1 = Dictionaries.realtion.FindIndex(s => s.relationCode == data.code);
        if (index1 == -1)
        {
            Relation relation = new Relation();
            relation.relationCode = data.code;
            Dictionaries.realtion.Add(relation);
            保存关键逻辑串码();
        }
        //Dictionaries.relationpath.Add();
    }
    public void 删除关系()
    {
        string[] strs = data.code.Split('@');
        int index = Dictionaries.relationpath.FindIndex(s => s.code == strs[strs.Length - 1]);
        if (index != -1)
        {
            Dictionaries.relationpath.RemoveAt(index);
            保存关系数据();
        }
        int index1 = Dictionaries.realtion.FindIndex(s => s.relationCode == data.code);
        if (index != -1)
        {
            Dictionaries.realtion.RemoveAt(index1);
            保存关键逻辑串码();
        }
        DestroyImmediate(data.gameObject);
    }
    void 保存关系数据()
    {
        Dictionaries.relationPathJson pathjson = new Dictionaries.relationPathJson();
        pathjson.path = Dictionaries.relationpath;
        //ES2.Save(pathjson.path,"RelationPath.config?tag=name");
        string jsonx = JsonUtility.ToJson(pathjson);
        string savePath = Application.dataPath + "/Resources/Config/RelationPath.json";
        File.WriteAllText(savePath, jsonx, Encoding.UTF8);
    }
    void 保存关键逻辑串码()
    {
        Dictionaries.RelationJson rjson = new Dictionaries.RelationJson();
        rjson.relation = Dictionaries.realtion;
        //ES2.Save(rjson.relation,"Relation.config?tag=name");
        string jsonr = JsonUtility.ToJson(rjson);
        string savePathr = Application.dataPath + "/Resources/Config/Relation.json";
        File.WriteAllText(savePathr, jsonr, Encoding.UTF8);
    }

}
